package org.tau.workshop2011.showcase

import org.tau.workshop2011.codegen.{WidgetGenerator, Property}
import javax.swing.{SpringLayout, JFrame, WindowConstants, JComponent}
import java.awt.{Dimension, GridBagConstraints, GridBagLayout, GridLayout}

case class EmailCode(Email: String, UserName: String, Password: String) {

  //
  protected def SendmailWindow(generator: WidgetGenerator, MAIL: ScalaEmail, to: String, subject: String, content: String, width: Int = 600, height: Int = 400) {
    var input: Map[String, Any] = Map()
    input += "width" -> width
    input += "height" -> height

    input += "readContent" -> content
    input += "readSender" -> to
    input += "readSubject" -> subject

    val pair = generator("reply_window")(input)
    val MainComp = pair._1
    val Props = pair._2

    import javax.swing.{JComponent, JFrame}
    val f = new JFrame()
    f.setLayout(new java.awt.GridLayout(1,1))

    Props("SubmitC").asInstanceOf[Property[Boolean]].addChangeListener {(prop, old, newval) => {
      if (newval) {
        val to = Props("readSender").asInstanceOf[Property[String]].get
        val content = Props("readContent").asInstanceOf[Property[String]].get
        val subject = Props("readSubject").asInstanceOf[Property[String]].get
        MAIL.sendEmail(to, subject, content)
        f.setVisible(false)
        f.dispose()
      }
    }}
    

    f.setLayout(new java.awt.GridBagLayout())
    val layout = new java.awt.GridBagLayout()
    val const = new java.awt.GridBagConstraints()
    const.fill = java.awt.GridBagConstraints.BOTH
    const.gridx = 1
    const.gridy = 1
    const.gridwidth = 1
    const.gridheight = 1
    layout.addLayoutComponent(MainComp, const)
    f.add(MainComp)
    f.pack()
    f.setVisible(true)
    f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
  }

  var ActiveProp: Property[Int] = null
  def RunEmailProgram(generator: WidgetGenerator, width: Int = 800, height: Int = 400) {
    val MAIL = new ScalaEmail(Email, UserName, Password)
    var emails = MAIL.batchFetch(1,25)

    import javax.swing.{JComponent, JFrame}
    val f = new JFrame()

    var input: Map[String, Any] = Map()
    input += "width" -> width
    input += "height" -> height

    import org.tau.workshop2011.expressions.Color
    val EEbgcol: (Any*) => Color = (args) => new Color("0xbbddff")
    input += "EEbgcol" -> (EEbgcol)

    val EEfgcol: (Any*) => Color = (args) => new org.tau.workshop2011.expressions.Color("0x000000")
    input += "EEfgcol" -> (EEfgcol)

    val EmailSubject: (Any*) => String = (args) => {
      val i: Int = args.head.asInstanceOf[Int]
      if (i < emails.length)
        emails(i).getSubject()
      else
        MAIL.emailSubject(i+1)
    }
    input += "EmailSubject" -> (EmailSubject)

    val EmailSender: (Any*) => String = (args) => {
      val i: Int = args.head.asInstanceOf[Int]
      if (i < emails.length)
        emails(i).getFrom()
      else
        MAIL.emailSender(i+1)
    }
    input += "EmailSender" -> (EmailSender)

    val EmailContent: (Any*) => String = (args) => {
      val i: Int = args.head.asInstanceOf[Int]
      if (i < emails.length)
        emails(i).getContent()
      else
        MAIL.emailContent(i+1)
    }
    input += "EmailContent" -> (EmailContent)

    val Activate: (Any*) => Int = (args) => {
      val i = args.head.asInstanceOf[Int]
      val clicked = args.tail.head.asInstanceOf[Boolean]
      if (clicked && ActiveProp != null)
        ActiveProp.set(i)

      1 /* Just some value */
    }
    input += "Activate" -> (Activate)

    val pair = generator("main_window")(input)
    val MainComp = pair._1
    val Props = pair._2

    ActiveProp = Props("Active").asInstanceOf[Property[Int]]

    Props("ReplyC").asInstanceOf[Property[Boolean]].addChangeListener {(prop, oldval, newval) => {
      if (newval) {
        val from = Props("readSender").asInstanceOf[Property[String]].get
        val content = Props("readContent").asInstanceOf[Property[String]].get
        val subject = "RE: " + Props("readSubject").asInstanceOf[Property[String]].get
        SendmailWindow(generator, MAIL, from, subject, content)
      }
    }}

    Props("NewC").asInstanceOf[Property[Boolean]].addChangeListener {(prop, oldval, newval) => {
      if (newval) {
        SendmailWindow(generator, MAIL, "", "", "")
      }
    }}

    Props("RefreshC").asInstanceOf[Property[Boolean]].addChangeListener {(prop, oldval, newval) => {
      if (newval) {
        emails = MAIL.batchFetch(1,25)
        // Force update on the subjects
        Props("EmailSubject").asInstanceOf[Property[(Any*) => String]] set EmailSubject
        Props("EmailSender").asInstanceOf[Property[(Any*) => String]] set EmailSender
        Props("EmailContent").asInstanceOf[Property[(Any*) => String]] set EmailContent
      }
    }}

    val layout = new javax.swing.SpringLayout
    f.getContentPane.setLayout(layout)
    f.getContentPane.add(MainComp)
    layout.putConstraint(javax.swing.SpringLayout.WEST, MainComp, 0, javax.swing.SpringLayout.WEST, f.getContentPane)
    layout.putConstraint(javax.swing.SpringLayout.EAST, MainComp, 0, javax.swing.SpringLayout.EAST, f.getContentPane)
    layout.putConstraint(javax.swing.SpringLayout.NORTH, MainComp, 0, javax.swing.SpringLayout.NORTH, f.getContentPane)
    layout.putConstraint(javax.swing.SpringLayout.SOUTH, MainComp, 0, javax.swing.SpringLayout.SOUTH, f.getContentPane)
    f.getContentPane.setMinimumSize(new Dimension(width, height))
    f.getContentPane.setMaximumSize(new Dimension(width, height))
    f.getContentPane.setPreferredSize(new Dimension(width, height))
    f.pack()
//    f.setSize(width, height)
//    f.setResizable(false)
    f.setVisible(true)
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  }

}